package com.hzncc.scaffold.controller.Rear.Other.Major;

import cn.hutool.core.collection.CollUtil;
import com.hzncc.scaffold.core.ResultT;
import com.hzncc.scaffold.entity.Other.Course;
import com.hzncc.scaffold.entity.Other.Major;
import com.hzncc.scaffold.entity.Vo.MajorIdVo;
import com.hzncc.scaffold.entity.Vo.UpdateMajorVo;
import com.hzncc.scaffold.mapper.MajorMapper;
import com.hzncc.scaffold.service.MajorService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.ReactiveZSetCommands;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: DrunkBear
 * @Date: 2022-06-21 14:31
 * @Description: null
 */
@RestController
@Api(tags = "后台接口-专业")
@RequestMapping("/manage/major")
public class MajorController {


    @Autowired
    MajorMapper majorMapper;

    @Autowired
    MajorService majorService;

    @GetMapping("/checkUsed")
    @ApiOperation("检查模块是否能修改")
    public ResultT checkUsed(Integer majorId) {
        List<Course> courses = majorMapper.checkUsed(majorId);

        if (CollUtil.isNotEmpty(courses)) {
            Boolean used = true;
            return ResultT.success(used);
        } else {
            Boolean used = false;
            return ResultT.success(used);
        }
    }

    @PostMapping("/update")
    @ApiOperation("修改模块")
    public ResultT update(@RequestBody UpdateMajorVo updateMajorVo) {
        Integer majorId = updateMajorVo.getMajorId();

        if (majorService.getById(majorId) == null) {
            return ResultT.fail("数据错误");
        }

        Major major = majorService.getById(majorId);
        major.setName(updateMajorVo.getName());
        major.setPid(updateMajorVo.getPid());

        boolean update = majorService.updateById(major);

        if (update) {
            return ResultT.success("操作成功");
        } else {
            return ResultT.fail("操作失败");
        }
    }

    @PostMapping("/add")
    @ApiOperation("新增模块")
    public ResultT add(@RequestBody Major major) {

        boolean save = majorService.save(major);

        if (save) {
            return ResultT.success("操作成功");
        } else {
            return ResultT.fail("操作失败");
        }
    }

    @PostMapping("/delete")
    @ApiOperation("删除模块")
    public ResultT delete(@RequestBody MajorIdVo majorIdVo) {
        Integer majorId = majorIdVo.getMajorId();

        boolean remove = majorService.removeById(majorId);

        if (remove) {
            return ResultT.success("操作成功");
        } else {
            return ResultT.fail("操作失败");
        }
    }
}
